*This file performs local level analysis and creates Table 6 and Figure 5.
*Within the file runs occupational_structure.do
*Also creates robustness tables D.8-D.10

clear all

******
*Input the municipal-level industry dataset
*****

use "$path/manufacturing_data", clear

// Create lagged controls
local years 1938 1940 1943

foreach year of local years {
    preserve
    keep if year == `year'

    // Generate year-specific suffix
    local suffix = "`year'"
    
    // Generate new variables with year-specific suffixes
    gen salary_`suffix' = salaryworker
    gen other_`suffix' = otherpersonnel
    gen grossvalueproduct_`suffix' = grossvalueproduct
    gen workersmean_`suffix' = workersmean 
    gen factorynumber_`suffix' = factorynumber 
    gen horsepower_`suffix' = horsepower 

    // Generate new variables with transformations
    gen ln_otherpersonnel_`suffix' = ln(otherpersonnel + 1)
    gen ln_valueadded_`suffix' = ln(valueadded + 1)
    gen ln_hoursworkers_`suffix' = ln(hoursworkers)
    gen ln_salary_`suffix' = ln(salary_`suffix')
    gen ln_grossvalueproduct_`suffix' = ln(grossvalueproduct_`suffix' + 1)
    replace ln_grossvalueproduct_`suffix' = 0 if ln_grossvalueproduct_`suffix' == .
    replace ln_salary_`suffix' = 0 if ln_salary_`suffix' == .
    gen ln_workersmean_`suffix' = ln(workersmean_`suffix')
    gen ln_factorynumber_`suffix' = ln(factorynumber_`suffix')
    gen ln_horsepower_`suffix' = ln(horsepower_`suffix' + 1) 
    gen employment_`suffix' = otherpersonnel + workersmean

    // Keep necessary variables
    keep ln_salary_`suffix' ln_grossvalueproduct_`suffix' employment_`suffix' other_`suffix' ln_otherpersonnel_`suffix' grossvalueproduct_`suffix' ln_workersmean_`suffix' workersmean_`suffix' factorynumber_`suffix' ln_factorynumber_`suffix' ln_valueadded_`suffix' ln_hoursworkers_`suffix' ln_horsepower_`suffix' code
    
    // Save the file
    tempfile controls`suffix'
    save `controls`suffix''

    restore
}

// Perform the join operations
count
joinby code using "`controls1943'", unmatched(none)
joinby code using "`controls1938'", unmatched(none)
joinby code using "`controls1940'", unmatched(none)

*Local shift share shocks
joinby code using  "$path/bartik_shocks.dta", unmatched(none)

*Import full sample occupational structure controls 
preserve
use "$path/occupational_structure_data_full", clear
keep if year==1940
gen manushare_a1940=manushare_a
gen agrishare_a1940=agrishare_a
gen ln_pop_1940=ln_pop
keep code manushare_a1940 agrishare_a1940 ln_pop_1940
tempfile controls
save `controls'
restore

joinby code using `controls'

gen bartik_standardized=bartik_st

tab year, gen(y)

forval j = 1/33 { 
    rename y`j' y`= `j' + 1933' 
} 

forval num = 1934/1942 {
gen bartikX`num'=bartik_standardized*y`num'
}

forval num = 1944/1966 {
gen bartikX`num'=bartik_standardized*y`num'
}

*Elastic net controls

preserve
keep if year==1943

set seed 1608
elasticnet linear bartik_st ln_pop_1940 manushare_a1940 agrishare_a1940  ln_pop_1940  ln_workersmean_1943 ln_valueadded_1943 ln_salary_1943 ln_otherpersonnel_1943 ln_grossvalueproduct_1943 ln_factorynumber_1943 swedish_share_1940 share_service_1940 relative_tractors1940 relative_cows1940 ln_arable_1940 ln_pop_1940 income_tax_payers38 relative_tractors1940 displaced_share_1940 

lassoinfo
lassocoef

global enet_12   c.agrishare_a1940#year c.ln_valueadded_1943#year c.ln_grossvalueproduct_1943#year c.swedish_share_1940#year c.relative_cows1940#year c.income_tax_payers38#year  

restore

*Define controls
global baseline2 c.manushare_a1940#year c.agrishare_a1940#year

global industry   c.ln_workersmean_1943#year c.ln_factorynumber_1943#year  c.ln_salary_1943#year c.ln_valueadded_1943#year c.ln_otherpersonnel_1943#year c.ln_grossvalueproduct_1943#year 

global controls40 c.relative_tractors1940#year  c.displaced_share_1940#year c.ln_pop_1940#year c.swedish_share_1940#year  c.share_service_1940#year c.relative_tractors1940#year c.relative_cows1940#year c.ln_arable_1940#year c.income_tax_payers38#year 

global industry38   c.ln_workersmean_1938#year c.ln_factorynumber_1938#year  c.ln_salary_1938#year  

gen group0=(year>1933&year<1943)
gen group1=(year>1943&year<1953)
gen group2=(year>1952&year<1961)
gen group3=(year>1960&year<1971)

gen treatgroup0=group0*bartik_standardized
gen treatgroup1=group1*bartik_standardized
gen treatgroup2=group2*bartik_standardized
gen treatgroup3=group3*bartik_standardized

label var treatgroup0 "1934-1942 x Exposure"
label var treatgroup1 "1944-1952 x Exposure"
label var treatgroup2 "1953-1960 x Exposure"
label var treatgroup3 "1961-1966 x Exposure"

local repaflexible1 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "

tempfile t1

*Define interactions
global treat bartikX1934 bartikX1935 bartikX1936 bartikX1937 bartikX1938 bartikX1939 bartikX1940 bartikX1941 bartikX1942 bartikX1944 bartikX1945 bartikX1946 bartikX1947 bartikX1948 bartikX1949 bartikX1950 bartikX1951 bartikX1952 bartikX1953 bartikX1954 bartikX1955 bartikX1956 bartikX1957 bartikX1958 bartikX1959 bartikX1960 bartikX1961 bartikX1962 bartikX1963 bartikX1964 bartikX1965 bartikX1966

parmby  "reghdfe ln_workersmean  $treat [aw=wei] , a(year#laani50 year#city year#town code $enet_12) cl(code)", lab saving(`"`t1'"',replace) idn(1) ids(Unadjusted)

********************************
*Event study Panel A of Figure 5
********************************

preserve

clear
set obs 1
gen year =1943
gen estimate = 0
gen parm ="bartikX1943"
gen min95 =0
gen max95 =0
append using `t1'
*keep if regexm(parm, "^repa*")
gen yearstr =substr(parm, -4,4)
destring yearstr, replace force
sort yearstr
drop if parm=="_cons"

twoway (scatter estimate yearstr , msize(medium) mcolor(dknavy)) (rspike   min95 max95 yearstr , lcolor(dknavy)),  xsize(6) ysize(4) xtitle(Year, ) xline(1944, lcolor(maroon)) xline(1952, lcolor(maroon)) title("Panel B") ///
xlabel(1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966, ) plotregion(lcolor(black)) graphregion(lcolor(black) fcolor(white) color(white)) legend(off)  xlabel(#8)  xlabel(,grid nogextend ) ylabel(,grid nogextend)  ytitle("Estimates of ln Manufacturing Employment", margin(0 1 0 0))   saving("$graphs/workers.gph", replace)

*Black and white 

twoway  (rspike   min95 max95 yearstr , lcolor(gs10)) (scatter estimate yearstr , msize(medium) mcolor(gs0)) ,  xsize(6) ysize(4) xtitle(Year, ) xline(1944, lcolor(gs10)) xline(1952, lcolor(gs10)) title("Panel B")  ///
xlabel(1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966, ) plotregion(lcolor(black)) graphregion(lcolor(black) fcolor(white) color(white)) legend(off)  xlabel(#8)  xlabel(,grid nogextend ) ylabel(,grid nogextend)  ytitle("Estimates of ln Manufacturing Employment", margin(0 1 0 0))   saving("$graphs/workers_bw.gph", replace)
 
restore


*******************
*Estimations for Table 6
*******************

gen post=(year>1943)

gen bartik_post=post*bartik_standardized

gen bartik_h_post=post*bartik_st_H
gen bartik_l_post=post*bartik_st_L
gen bartik_ntp_st_post=post*bartik_ntp_st

label var bartik_post "Exposure x Post"
label var bartik_h_post "HS Exposure x Post"
label var bartik_l_post "LS Exposure x Post"

local repaflexible1 " bartik_post"


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#laani50 $baseline2 year#city  year#town code)  vce(cluster code) 
estadd local enet " "
estadd local Municipalities " 204 "
estadd local established "Yes"

estimates store a`x' 
}

local repaflexible1 "bartik_post"

foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#laani50 $enet_12 year year#city year#town code)  vce(cluster code) 
estadd local enet "\checkmark"
estadd local Municipalities " 204 "
estadd local established "Yes"

estimates store b`x' 
}

**HS and LS


local repaflexible1 "bartik_h_post bartik_l_post"


foreach x in ln_workersmean  ln_salary ln_grossvalueproduct ln_factorynumber  {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#laani50  $baseline2 year#city  year#town code)  vce(cluster code) 
estadd local enet " "
estadd local Municipalities " 204 "
estadd local established "Yes"

estimates store ahl`x' 
}


local repaflexible1 "bartik_h_post bartik_l_post"


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#laani50 $enet_12 year year#city year#town code)  vce(cluster code) 
estadd local enet "\checkmark"
estadd local Municipalities " 204 "
estadd local established "Yes"

estimates store bhl`x' 
}

*Without established 


preserve 
replace bartik_post=bartik_ntp_st_post
foreach x in ln_workersmean  ln_salary ln_grossvalueproduct ln_factorynumber  {
reghdfe `x' bartik_post [aw=wei] ,a(year#laani50  $baseline2 year#city  year#town code)  vce(cluster code) 
estadd local enet " "
estadd local established "No"

estadd local Municipalities " 204 "
estimates store antp`x' 
}
restore 

preserve 
replace bartik_post=bartik_ntp_st_post
foreach x in ln_workersmean  ln_salary ln_grossvalueproduct ln_factorynumber  {
reghdfe `x' bartik_post [aw=wei] ,a(year#laani50  $enet_12 year#city  year#town code)  vce(cluster code) 
estadd local enet "\checkmark"
estadd local established "No"

estadd local Municipalities " 204 "
estimates store bntp`x' 
}
restore 


*Robustness table estiamtions

local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year code year#city year#town year#laani50  $baseline2)  vce(cluster code) 
estadd local industry1 ""
estadd local controls ""
estadd local enet ""
estadd local baseline "\checkmark"
estadd local lfe "\checkmark"
estadd local kunta "204" 
estimates store  e`x' 
}


local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "

foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible2' [aw=wei] ,a( year  code year#city year#town year#laani50    $baseline2)  vce(cluster code) 
estadd local industry1 ""
estadd local controls ""
estadd local enet ""
estadd local baseline "\checkmark"
estadd local lfe "\checkmark"
estadd local kunta "204" 
estimates store e1`x' 
}


local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#laani50  year code $enet_12)  vce(cluster code) 
estadd local industry1 ""
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe "\checkmark"
estadd local kunta "204" 
estimates store  f`x' 
}


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible2' [aw=wei] ,a( year#laani50  year code $enet_12)  vce(cluster code) 
estadd local industry1 ""
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe "\checkmark"
estadd local kunta "204" 
estimates store f1`x' 
}


local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#city year#town year code $enet_12)  vce(cluster code) 
estadd local industry1 ""
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe ""
estadd local kunta "204" 
estimates store  g`x' 
}


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible2' [aw=wei] ,a( year#city year#town year code $enet_12)  vce(cluster code) 
estadd local industry1 ""
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe ""
estadd local kunta "204" 
estimates store g1`x' 
}

local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "


local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "

foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#laani50 year#city year#town code  $industry)  vce(cluster code) 
estadd local kunta "204" 
estadd local industry1 "\checkmark"
estadd local controls ""
estadd local enet ""
estadd local lfe "\checkmark"
estimates store c`x' 
}


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible2' [aw=wei] ,a(year#laani50 year#city year#town code  $industry)  vce(cluster code) 
estadd local industry1 "\checkmark"
estadd local controls ""
estadd local enet ""
estadd local lfe "\checkmark"
estadd local ufe "\checkmark" 
estadd local kunta "204" 
estimates store c1`x' 
}


local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "

foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei], a( $industry $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local baseline "\checkmark"
estadd local industry1 "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "204" 

estimates store v`x' 
}


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible2' [aw=wei], a( $industry $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local baseline "\checkmark"
estadd local industry1 "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "204" 
estimates store v1`x' 
}


local repaflexible1 "bartik_post"
local repaflexible2 " treatgroup0 treatgroup1 treatgroup2 treatgroup3 "


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible1' [aw=wei] ,a(year#laani50 year#city year#town code $baseline2 $industry $controls40)  vce(cluster code) 
estadd local industry1 "\checkmark"
estadd local controls "\checkmark"
estadd local enet ""
estadd local lfe "\checkmark"
estadd local kunta "204" 
estimates store d`x' 
}


foreach x in ln_workersmean  ln_factorynumber   {
reghdfe `x' `repaflexible2' [aw=wei] ,a(year#laani50 year#city year#town code $baseline2 $industry $controls40)  vce(cluster code) 
estadd local industry1 "\checkmark"
estadd local controls "\checkmark"
estadd local enet ""
estadd local lfe "\checkmark"
estadd local kunta "204" 
estimates store d1`x' 
}


esttab eln_workersmean e1ln_workersmean  fln_workersmean f1ln_workersmean gln_workersmean g1ln_workersmean  cln_workersmean c1ln_workersmean vln_workersmean v1ln_workersmean dln_workersmean d1ln_workersmean  eln_factorynumber e1ln_factorynumber  fln_factorynumber f1ln_factorynumber gln_factorynumber g1ln_factorynumber cln_factorynumber c1ln_factorynumber   vln_factorynumber v1ln_factorynumber dln_factorynumber d1ln_factorynumber ///
using   "$tables/DD_flexible_estimates_municipality_robust_2.tex",  ///
mgroups("ln Manufacturing Workers" "ln Establishments"  ,   pattern(1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) ) ///
replace  keep(`repaflexible1' `repaflexible2') nonotes ///
b(%9.3f) se(%9.3f) stats(N kunta baseline  lfe enet  industry1 controls, fmt(0 3) layout(@ @ @ @) ///
labels("N" "Municipalities" "Baseline controls" "Region FE"  "Elastic net controls"  "1943 Industry controls" "1940 municipal controls"   ///
)) ///
label star(* .1 ** .05 *** .01) nodepvars nomtitle 


*save the controls to be use in occupational structure robustness checks

preserve
keep if year==1943
keep ln_salary_1943 ln_grossvalueproduct_1943 employment_1943 other_1943 ln_otherpersonnel_1943 grossvalueproduct_1943 ln_workersmean_1943 workersmean_1943 factorynumber_1943 ln_factorynumber_1943 ln_valueadded_1943 ln_hoursworkers_1943 code
tempfile industry_controls
save "$path/industry_controls" , replace
restore


save "$path/industry_forregs.dta", replace


**Run the the occupational structure estimation and get the estimates to combine for table 6 and figure 5

do "$path/occupational_structure.do"


*Save combined datasets

save "$path/elinkeino_combined_forregs.dta", replace


*Generate Figure 5
set graphics on

cd "$graphs"

graph combine manushare.gph workers.gph,  col(1) iscale(.6) ysize(7) xsize(5) commonscheme  graphregion(fcolor(white) color(white))

graph export "$graphs/combined_muni_impacts.eps",as(eps) font(Times) replace
graph export "$graphs/combined_muni_impacts.pdf",as(pdf) font(Times) replace

graph combine manushare_bw.gph workers_bw.gph,  col(1) iscale(.6) ysize(7) xsize(5) commonscheme  graphregion(fcolor(white) color(white))

graph export "$graphs/combined_muni_impacts_bw.eps",as(eps) font(Times) replace
graph export "$graphs/combined_muni_impacts_bw.pdf",as(pdf) font(Times) replace
